package com.kbbywyclient.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.WithdrawRecord;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.WithdrawRecordModule;
import com.kbbywyclient.presenter.WithdrawRecordPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.adapter.base.BaseAdapter;
import com.kbbywyclient.ui.contarct.WithdrawMoneyContract;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomToolBar;
import com.kbbywyclient.widget.divider.VerticalItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by Jiangzx on 2017/9/6 15:24.
 * 提现记录
 */

public class WithdrawMoneyRecordActivity extends BaseActivity implements WithdrawMoneyContract.View{
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.sr_layout)
    SmartRefreshLayout srLayout;
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;

    private BaseAdapter<WithdrawRecord> adapter;
    @Inject
    WithdrawRecordPresenter presenter;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_withdraw_money_record;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->finish());
        srLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.page++;
                presenter.loadData(PreUtils.getUser(WithdrawMoneyRecordActivity.this).getSalesID(),
                        presenter.page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                clearList();
                presenter.loadData(PreUtils.getUser(WithdrawMoneyRecordActivity.this).getSalesID(),
                        presenter.page);
            }
        });
        LinearLayoutManager layoutManager=
                new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        VerticalItemDecoration decoration=new VerticalItemDecoration(Constants.DIVIDER_COLOR,1);
        recyclerView.addItemDecoration(decoration);
    }

    @Override
    public void initData() {
        srLayout.autoRefresh(500);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("提现记录");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getWithdrawRecordComponent(new WithdrawRecordModule(this)).inject(this);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadDataSuc(List<WithdrawRecord> data) {
        if(adapter==null){
            adapter=new BaseAdapter<WithdrawRecord>(data,R.layout.item_withdraw_money_record) {
                @Override
                public void convert(Holder holder, WithdrawRecord withdrawRecord, int position) {
                    holder.setText(R.id.tv_amount,"-￥"+withdrawRecord.getAmount());
                    TextView status=holder.getView(R.id.tv_status);
                    //处理状态
                    switch (withdrawRecord.getStatus()){
                        case 1:
                            //审核中
                            status.setTextColor(0XFF1EC29A);
                            status.setText("审核中");
                            break;
                        case 2:
                            //审核通过
                            switch (withdrawRecord.getPay_status()){
                                case "SUCCESS":
                                    //成功
                                    status.setTextColor(0XFF999999);
                                    status.setText("提现成功");
                                    break;
                                case "FAILURE":
                                    //失败
                                    status.setTextColor(0XFFFB576A);
                                    String payRemark=withdrawRecord.getPay_remark();
                                    if(TextUtils.isEmpty(payRemark)){
                                        status.setText("提现失败");
                                    }else{
                                        status.setText("提现失败("+payRemark+")");
                                    }
                                    break;
                                case "PROCESSING":
                                default:
                                    status.setTextColor(0XFF1EC29A);
                                    status.setText("提现处理中");
                                    break;
                            }
                            break;
                        case 3:
                        default:
                            //审核未通过
                            status.setTextColor(0XFFFB576A);
                            String remark=withdrawRecord.getRemark();
                            if(TextUtils.isEmpty(remark)){
                                status.setText("未通过");
                            }else{
                                status.setText("未通过("+remark+")");
                            }
                            break;
                    }
                    holder.setText(R.id.tv_date,withdrawRecord.getCreate_time());
                }
            };
            recyclerView.setAdapter(adapter);
        }else{
            adapter.setDataSource(data);
        }
    }

    @Override
    public void loadDataComplete() {
        srLayout.finishRefresh();
        srLayout.finishLoadmore();
    }

    @Override
    public void setCanLoadMore(boolean canLoadMore) {
        srLayout.setLoadmoreFinished(!canLoadMore);
    }

    private void clearList(){
        presenter.clearData();
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }
}
